package com.lan;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.text.NumberFormat;
import java.util.*;

@SpringBootApplication
@MapperScan("com.lan.mapper")
public class RunApplication
{
    public static void main(String[] args) throws IOException {
//        SpringApplication.run(RunApplication.class, args);
        test();
    }

    private static void test() throws IOException {
        set("doc1.txt");
        set("doc2.txt");

        List<String> strings1 = get("doc1.txt");
        List<String> strings2 = get("doc2.txt");

        //计算交集 得出相似度
        Double i = Double.parseDouble(CollectionUtils.intersection(strings1, strings2).size() +"") / (strings1.size() * strings2.size());
        System.out.println(i);
    }

    /**
     * 生成文件.
     *
     * */
    private static List<String> get(String name) throws IOException {
        //将fos.txt文件中所有字符读取回来
        File file = new File(name);
        FileInputStream fis = new FileInputStream(file);
        byte[] data2 = new byte[(int)file.length()];
        fis.read(data2);
        String line2 = new String(data2, StandardCharsets.UTF_8);
        fis.close();
        return Arrays.asList(line2.split(" "));
    }

    /**
     * 获取文件.
     *
     * */
    private static void set(String name) throws IOException {
        FileOutputStream fos = new FileOutputStream(name,true);
        String line = "";
        byte[] data = line.getBytes(StandardCharsets.UTF_8);
        fos.write(data);
        for (int i = 0; i < 10; i++) {
            line += " "+UUID.randomUUID().toString();
        }
        data = line.getBytes(StandardCharsets.UTF_8);
        fos.write(data);
        fos.close();
    }

}